package com.zqx.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqx.domain.*;
import com.zqx.service.DormService;
import com.zqx.service.FixService;
import com.zqx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Controller
@RequestMapping("fix")
public class FixController {
    @Autowired
    private FixService fixService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private DormService dormService;

    @RequestMapping("listFix")
    public String listMove(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4", required = false) Integer pageSize, Fix fix, Model model) {
        //分页start
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize < 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        //分页end
        //模糊查询，搜索start
        QueryWrapper<Fix> qw = new QueryWrapper<>();
        if (fix.getSname() != null) {
            qw.like("sname", fix.getSname());
            //当点击搜索按钮，sname将被赋予值，qw会存储后进行模糊查询
        }
        List<Fix> list = fixService.list(qw);
        for (Fix fix1:list
             ) {
            Dorm byId = dormService.getById(fix1.getDid());
            fix1.setDorm(byId.getBuild()+"-"+byId.getDormid());
        }
        PageInfo<Fix> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        //根据宿舍信息查询所有楼层来做复选框
        List<Student> list1 = studentService.list(null);
        model.addAttribute("studentList", list1);
        return "admin-fix-list";
    }
    @RequestMapping("saveFix")
    public String save(Fix fix){
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("sname", fix.getSname());
        Student one = studentService.getOne(qw);
        fix.setSimage(one.getSimage());
        fix.setStatus(0);
        fix.setDid(one.getDid());
        boolean save = fixService.save(fix);
        return "redirect:/fix/listFix";
    }
    //维修
    @RequestMapping("Fix/{id}")
    public String Fix(@PathVariable Integer id){
        Fix byId = fixService.getById(id);
        byId.setStatus(1);
        fixService.updateById(byId);
        return "redirect:/fix/listFix";
    }
    @RequestMapping("delFix/{id}")
    public String delFix(@PathVariable Integer id){
        boolean b = fixService.removeById(id);
        return "redirect:/fix/listFix";
    }
    @RequestMapping("batchDeleteFix")
    public String batchDeleteFix(String idList){
        String[] split = StrUtil.split(idList, ",");
        List<Integer> list = new ArrayList<>();
        for (String s: split) {
            if (!s.isEmpty()){
                list.add(Integer.valueOf(s));
            }
        }
        boolean b = fixService.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }
}
